#include "for_vectors.h"

int exact_left_turn(const vect& a,const point& b)
{
   /*Old exact_left_turn. This can be useful if I know that all I'm gonna write now is some piece of crap.*/

   std::vector<double> temp;
   fill(temp,a,b);
   return sum_heaps(temp);
   /*std::vector<double> temp;
   fill(temp,a,b);

   std::vector<double> result;
   result.push_back(temp[1]); result.push_back(temp[0]);
   for (int i = 1; i < 6; ++i)
   {
      sum_with_roundoffs(result,temp[2 * i + 1],0);
      sum_with_roundoffs(result,temp[2 * i],1);
   }

   for (int i = result.size() - 1; i > -1; --i)
   {
      if (result[i] != 0)
      {
         return ((int) (result[i] / std::abs(result[i]))); //exact sign of turn
      }
   }*/

   return 0; //Who-o-ops...
}

bool bounding_box(const vect& a,const vect& b)
{
   if (((a.a.x > b.a.x) && (a.a.x > b.b.x) && (a.b.x > b.a.x) && (a.b.x > b.b.x)) || ((a.a.x < b.a.x) && (a.a.x < b.b.x) && (a.b.x < b.a.x) && (a.b.x < b.b.x)) || ((a.a.y > b.a.y) && (a.a.y > b.b.y) && (a.b.y > b.a.y) && (a.b.y > b.b.y)) || ((a.a.y < b.a.y) && (a.a.y < b.b.y) && (a.b.y < b.a.y) && (a.b.y < b.b.y))) {
      return false;
   } else {
      return true;
   }
}

int left_turn(const vect& a,const point& b)
{
   /* Old left_turn. This be useful if I know that all I'm gonna write now is some piece of crap.
   
   std::vector<std::pair<double,double> > muls;
   muls.push_back(mul(a.b.x, b.y));
   muls.push_back(mul(-a.b.x, a.a.y));
   muls.push_back(mul(-a.a.x, b.y));
   muls.push_back(mul(-a.b.y, b.x));
   muls.push_back(mul(a.b.y, a.a.x));
   muls.push_back(mul(a.a.y, b.x));

   double turn = 0,r_offs = 0;

   for (size_t i = 0; i < muls.size(); ++i)
   {
      turn += muls[i].first;
      r_offs += muls[i].second;
   }

   if (std::abs(turn) - std::abs(r_offs) <= 0) {
      return exact_left_turn(a,b);
   } else {
      return (int) (turn / std::abs(turn));
   }*/

   double tmp1 = (a.b.x - a.a.x) * (b.y - a.a.y);
   double tmp2 = (a.b.y - a.a.y) * (b.x - a.a.x);

   double error = 8 * std::numeric_limits<double>::epsilon() * (std::abs(tmp1) + std::abs(tmp2));

   double turn = tmp1 - tmp2;

   if (std::abs(turn) > error) {
      return ((int) (turn / std::abs(turn))); //just a sign of turn
   } else {
      return exact_left_turn(a,b);    //exact computation of left_turn(a,b)
   }
}